/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component.tabbedPane;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
/**
 * 说明：
 * 重写JPanel
 * @Author：李智深
 * @Date：2012-07-04
 */
public class RPTabHeadComponent extends JPanel {

	private static final long serialVersionUID = 1L;
    private Image tabBg = ImageFactory.loadImage(ImageFactory.TAB_BG).getImage();
    private Image tabSelectBg = ImageFactory.loadImage(ImageFactory.TAB_BG_SELECT).getImage();
    
    private JLabel lbTitle = new JLabel();
    private JTabbedPane tab = null;
    private Color selectedTitleColor = new Color(0, 105, 154);
    private Color unselectedTitleColor = new Color(100, 101, 102);
    private Border border = BorderFactory.createEmptyBorder(0, 5, 0, 5);

    public RPTabHeadComponent(JTabbedPane tab, String title) {
        this.tab = tab;
        lbTitle.setText(title);
        init();
        Util.setSize(this, 71, 27);
    }

    private void init() {
        lbTitle.setOpaque(false);
        lbTitle.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        lbTitle.setVerticalAlignment(SwingConstants.CENTER);
        lbTitle.setHorizontalAlignment(SwingConstants.CENTER);
        lbTitle.setFont(Util.FONT_12_BOLD);
        lbTitle.setForeground(Color.white);

        this.setLayout(new BorderLayout());
        this.add(this.lbTitle, BorderLayout.CENTER);
        this.setBorder(border);
        this.setOpaque(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        if (isTabSelected()) {
            g.drawImage(tabSelectBg, 0, 0, null);
        } else {
        	g.drawImage(tabBg, 0, 0, null);
        }
    }

//    @Override
//    public Dimension getPreferredSize() {
//        int width = super.getPreferredSize().width+10;
//        if (!this.isTabSelected()) {
////            width = Math.min(width, tab.getPreferredUnselectedTabWidth());
//        }
//        int height = tab.getPreferredTabHeight();
//        return new Dimension(width, height);
//    }

    public boolean isTabSelected() {
        int index = tab.indexOfTabComponent(this);
        int selectedIndex = tab.getSelectedIndex();
        return selectedIndex == index;
    }

    public void setTitle(String title) {
        this.lbTitle.setText(title);
    }
	@Override
	public Insets getInsets() {
		// TODO Auto-generated method stub
		return new Insets(0, 0, 0, 0);
	}
    public void updateSelection(boolean selected) {
        if (selected) {
            this.lbTitle.setForeground(selectedTitleColor);
        } else {
            this.lbTitle.setForeground(unselectedTitleColor);
        }
    }
}
